
package org.itboys.mobile.service;

import org.itboys.admin.entity.lasted.SystemDictionary;
import org.itboys.admin.service.lasted.SystemDictionaryService;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dao.product.SundayProductMapper;
import org.itboys.mobile.entity.mysql.order.SundayOrder;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mysql.front.SundayFrontOrderService;
import org.itboys.mobile.service.mysql.web.product.SundayProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_项目自动任务_权限。
 *
 */

@Service
public class SundayJobService {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private SystemDictionaryService dictionaryService;
	@Autowired
	private SundayFrontOrderService orderService;
	@Autowired
	private SundayProductMapper productMapper;


	/**
	 * 订单自动收货
	 */
	@Transactional
	public void autoReceive(){
		Integer autoReceiveDay=Integer.valueOf(dictionaryService.getDictionaryByKey("autoReceive").get(0).getValue());
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("status", SundayCommonConstants.ORDER_STATUS_PSZ);
		List<SundayOrder> orders = orderService.select(param);
		Calendar now =Calendar.getInstance();
		for(SundayOrder order:orders){
			if(order.getSendTime()!=null){
				Calendar sendTime = Calendar.getInstance();
				sendTime.setTime(order.getSendTime());
				sendTime.add(Calendar.DAY_OF_YEAR,10);
				if(sendTime.getTimeInMillis()<=now.getTimeInMillis()){
					try{
						orderService.confirmReceive(order.getId(),order.getMemberId());
					}catch (Exception e){
						logger.error("订单编号="+order.getOrderNo()+"，自动收货失败，原因："+e.getMessage());
					}
				}
			}
		}
	}

	/**
	 * 清空商品的今日销量.0点之下
	 */
	/*@Transactional
	public void autoClearTodaySale(){
		Map<String,Object> param = new HashMap<String,Object>();
		List<SundayProduct> products = productMapper.select(param);
		for(SundayProduct product:products){
			if(product.getTodaySale()!=null&&product.getTodaySale()>0){

				try{
					productMapper.clearTodaySale(product.getId());
				}catch (Exception e){
					logger.error("商品编号="+product.getBarCode()+"，清空今日销量失败，原因："+e.getMessage());
				}
			}
		}
	}*/
}